package com.example.hasee.zhcd.Adapters;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.hasee.zhcd.Activities.ChuanWu.ArrivalEntrustActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.ArrivalsDetailActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.LPEntrustActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.ShipDetailActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.WayBillActivity;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by hasee on 2017/9/20.
 */

public class ArrivalsListAdapter extends BaseAdapter {
    private HashMap<String, String> map = (HashMap<String, String>) GlobalParameter.get("loginmap");
    private ArrayList<HashMap<String, String>> list;
    private LayoutInflater itemInflater;
    private Context context;
    private Holder holder;

    public ArrivalsListAdapter(Context context, ArrayList<HashMap<String, String>> list) {
        this.context = context;
        this.list = list;
        this.itemInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            holder = new Holder();
            convertView = itemInflater.inflate(R.layout.arrivals_list_item, null);
            holder.shipname = (TextView) convertView.findViewById(R.id.shipname);
            holder.sailtime = (TextView) convertView.findViewById(R.id.sailtime);
            holder.workcompany = (TextView) convertView.findViewById(R.id.workcompany);
            holder.arrivaltime = (TextView) convertView.findViewById(R.id.arrivaltime);
            holder.berth = (TextView) convertView.findViewById(R.id.berth);
            holder.detail = (TextView) convertView.findViewById(R.id.detail);
            holder.fwbsx = (TextView) convertView.findViewById(R.id.fwbsx);
            holder.linear=(LinearLayout)convertView.findViewById(R.id.linear);
            holder.line=(View)convertView.findViewById(R.id.line);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.shipname.setText((String) list.get(position).get("fshipname"));
        holder.shipname.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
        holder.shipname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ShipDetailActivity.class);
                intent.putExtra("strSYN", list.get(position).get("fshipyearnumvcr"));
                context.startActivity(intent);
            }
        });
        holder.sailtime.setText((String) list.get(position).get("fshipordernum"));
        holder.workcompany.setText((String) list.get(position).get("fzygs"));
        holder.workcompany.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
        if (list.get(position).get("fzygs").length() != 0) {
            holder.workcompany.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, WayBillActivity.class);
                    intent.putExtra("strSYN", list.get(position).get("fshipyearnumvcr"));
                    context.startActivity(intent);
                }
            });
        }
        holder.arrivaltime.setText((String) list.get(position).get("fyddate"));
        holder.berth.setText((String) list.get(position).get("fberth"));
        if(list.get(position).get("fwbsx").length() == 0&&list.get(position).get("fshipyearnumvcr").length()==0){
            holder.linear.setVisibility(View.GONE);
            holder.line.setVisibility(View.GONE);
        }else{
            holder.linear.setVisibility(View.VISIBLE);
            holder.line.setVisibility(View.VISIBLE);
            if (list.get(position).get("fwbsx").length() == 0) {
                holder.fwbsx.setVisibility(View.INVISIBLE);
            } else {
                holder.fwbsx.setVisibility(View.VISIBLE);
                holder.fwbsx.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, ArrivalEntrustActivity.class);
                        intent.putExtra("fshipcode", list.get(position).get("fshipcode"));
                        intent.putExtra("fshipyearnumvcr", list.get(position).get("fshipyearnumvcr"));
                        context.startActivity(intent);
                    }
                });

            }
            if( list.get(position).get("fshipyearnumvcr").length()==0){
                holder.detail.setVisibility(View.GONE);
            }else{
                holder.detail.setVisibility(View.VISIBLE);
                holder.detail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, ArrivalsDetailActivity.class);
                        intent.putExtra("fshipyearnumvcr", list.get(position).get("fshipyearnumvcr"));
                        context.startActivity(intent);
                    }
                });
            }
        }
        return convertView;
    }

    class Holder {
        private TextView shipname;
        private TextView sailtime;
        private TextView workcompany;
        private TextView arrivaltime;
        private TextView berth;
        private TextView detail;
        private TextView fwbsx;
        private LinearLayout linear;
        private View line;

    }
}
